const app = getApp();

Page({
  data: {
    userInfo: {},
    isLogin: false,
    isMember: false,
    isRealNameAuth: false,
    memberExpireTime: '',
    likesCount: 0,
    commentsCount: 0
  },

  onLoad() {
    this.loadUserInfo();
  },

  onShow() {
    app.checkLoginStatus()
    setTimeout(() => {
        this.loadUserInfo();
    },100)
    
  },

  // 加载用户信息
  loadUserInfo() {
    const userInfo = app.globalData.userInfo;
    const userProfile = app.globalData.userProfile;
    
    if (userInfo) {
      this.setData({
        isLogin: true,
        userInfo: {
          ...userInfo,
          ...userProfile
        },
        isMember: app.globalData.isMember,
        isRealNameAuth: app.globalData.isRealNameAuth,
        memberExpireTime: app.globalData.memberExpireTime || ''
      });
    } else {
      this.setData({
        isLogin: false,
        userInfo: {},
        isMember: false,
        isRealNameAuth: false,
        memberExpireTime: ''
      });
    }

    this.loadStats();
  },

  // 加载统计数据
  loadStats() {
    if (!app.globalData.isLogin) return;
    
    // 模拟API调用
    setTimeout(() => {
      this.setData({
        likesCount: 25,
        commentsCount: 8
      });
    }, 300);
  },

  // 编辑资料
  editProfile() {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/register/register'
            });
          }
        }
      });
      return;
    }
    
    wx.navigateTo({
      url: '/pages/edit-profile/edit-profile'
    });
  },

  // 去会员页面
  goToMembership() {
    wx.navigateTo({
      url: '/pages/membership/membership'
    });
  },

  // 查看点赞
  viewLikes() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 查看评论
  viewComments() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 查看我的动态
  viewMyDynamics() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 查看微信号
  viewWechatId() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.showModal({
      title: '微信号',
      content: '微信号：love_sunset_2024',
      showCancel: false,
      confirmText: '复制',
      success: (res) => {
        if (res.confirm) {
          wx.setClipboardData({
            data: 'love_sunset_2024',
            success: () => {
              wx.showToast({
                title: '已复制到剪贴板',
                icon: 'success'
              });
            }
          });
        }
      }
    });
  },

  // 实名认证
  realNameAuth() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    
    if (app.globalData.isRealNameAuth) {
      wx.showToast({
        title: '已完成实名认证',
        icon: 'success'
      });
      return;
    }
    
    wx.navigateTo({
      url: '/pages/real-name-auth/real-name-auth'
    });
  },

  // 联系客服
  contactService() {
    wx.showModal({
      title: '联系客服',
      content: '客服电话：400-123-4567\n服务时间：9:00-18:00',
      showCancel: false,
      confirmText: '知道了'
    });
  },

  // 安全中心
  securityCenter() {
    if (!app.globalData.isLogin) {
      this.showLoginTip();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          app.logout();
          this.setData({
            isLogin: false,
            userInfo: {},
            isMember: false,
            isRealNameAuth: false,
            memberExpireTime: '',
            likesCount: 0,
            commentsCount: 0
          });
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  },

  // 显示登录提示
  showLoginTip() {
    wx.showModal({
      title: '提示',
      content: '请先登录',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/register/register'
          });
        }
      }
    });
  },

  onLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  }

}); 